﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;

namespace DMZS.DAL
{
    /// <summary>
    /// 数据访问类:Report_template
    /// </summary>
    public partial class Report_template
    {
        public Report_template()
        {
        }

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("F_ID", "Report_template");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int F_ID)
        {
            var strSql = new StringBuilder();
            strSql.Append("select count(1) from Report_template");
            strSql.Append(" where F_ID=@F_ID");
            var parameters = new SqlParameter[] { new SqlParameter("@F_ID", SqlDbType.Int, 4)
            };
            parameters[0].Value = F_ID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(DMZS.Model.Report_template model)
        {
            var strSql = new StringBuilder();
            strSql.Append("insert into Report_template(");
            strSql.Append("vchtype,tamname,creator,creatime,state,fileinfo)");
            strSql.Append(" values (");
            strSql.Append("@vchtype,@tamname,@creator,@creatime,@state,@fileinfo)");
            strSql.Append(";select @@IDENTITY");
            var parameters = new SqlParameter[] { new SqlParameter("@vchtype", SqlDbType.Int, 4),
                    new SqlParameter("@tamname", SqlDbType.VarChar, 50),
                    new SqlParameter("@creator", SqlDbType.Int, 4),
                    new SqlParameter("@creatime", SqlDbType.DateTime),
                    new SqlParameter("@state", SqlDbType.Int, 4),
                    new SqlParameter("@fileinfo", SqlDbType.Image) };
            parameters[0].Value = model.vchtype;
            parameters[1].Value = model.tamname;
            parameters[2].Value = model.creator;
            parameters[3].Value = model.creatime;
            parameters[4].Value = model.state;
            parameters[5].Value = model.fileinfo;

            var obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(DMZS.Model.Report_template model)
        {
            var strSql = new StringBuilder();
            strSql.Append("update Report_template set ");
            strSql.Append("vchtype=@vchtype,");
            strSql.Append("tamname=@tamname,");
            strSql.Append("creator=@creator,");
            strSql.Append("creatime=@creatime,");
            strSql.Append("state=@state,");
            strSql.Append("fileinfo=@fileinfo");
            strSql.Append(" where F_ID=@F_ID");
            var parameters = new SqlParameter[] { new SqlParameter("@vchtype", SqlDbType.Int, 4),
                    new SqlParameter("@tamname", SqlDbType.VarChar, 50),
                    new SqlParameter("@creator", SqlDbType.Int, 4),
                    new SqlParameter("@creatime", SqlDbType.DateTime),
                    new SqlParameter("@state", SqlDbType.Int, 4),
                    new SqlParameter("@fileinfo", SqlDbType.Image),
                    new SqlParameter("@F_ID", SqlDbType.Int, 4) };
            parameters[0].Value = model.vchtype;
            parameters[1].Value = model.tamname;
            parameters[2].Value = model.creator;
            parameters[3].Value = model.creatime;
            parameters[4].Value = model.state;
            parameters[5].Value = model.fileinfo;
            parameters[6].Value = model.F_ID;

            var rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int F_ID)
        {
            var strSql = new StringBuilder();
            strSql.Append("delete from Report_template ");
            strSql.Append(" where F_ID=@F_ID");
            var parameters = new SqlParameter[] { new SqlParameter("@F_ID", SqlDbType.Int, 4)
            };
            parameters[0].Value = F_ID;

            var rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string F_IDlist )
        {
            var strSql = new StringBuilder();
            strSql.Append("delete from Report_template ");
            strSql.Append(" where F_ID in (" + F_IDlist + ")  ");
            var rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DMZS.Model.Report_template GetModel(int F_ID)
        {
            var strSql = new StringBuilder();
            strSql.Append("select  top 1 F_ID,vchtype,tamname,creator,creatime,state,fileinfo from Report_template ");
            strSql.Append(" where F_ID=@F_ID");
            var parameters = new SqlParameter[] { new SqlParameter("@F_ID", SqlDbType.Int, 4)
            };
            parameters[0].Value = F_ID;
            var ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DMZS.Model.Report_template DataRowToModel(DataRow row)
        {
            var model = new DMZS.Model.Report_template();
            if (row != null)
            {
                if (row["F_ID"] != null && row["F_ID"].ToString() != string.Empty)
                {
                    model.F_ID = int.Parse(row["F_ID"].ToString());
                }
                if (row["vchtype"] != null && row["vchtype"].ToString() != string.Empty)
                {
                    model.vchtype = int.Parse(row["vchtype"].ToString());
                }
                if (row["tamname"] != null)
                {
                    model.tamname = row["tamname"].ToString();
                }
                if (row["creator"] != null && row["creator"].ToString() != string.Empty)
                {
                    model.creator = int.Parse(row["creator"].ToString());
                }
                if (row["creatime"] != null && row["creatime"].ToString() != string.Empty)
                {
                    model.creatime = DateTime.Parse(row["creatime"].ToString());
                }
                if (row["state"] != null && row["state"].ToString() != string.Empty)
                {
                    model.state = int.Parse(row["state"].ToString());
                }
                if (row["fileinfo"] != null && row["fileinfo"].ToString() != string.Empty)
                {
                    model.fileinfo = (byte[])row["fileinfo"];
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            var strSql = new StringBuilder();
            strSql.Append("select F_ID,vchtype,tamname,creator,creatime,state,fileinfo ");
            strSql.Append(" FROM Report_template ");
            if (strWhere.Trim() != string.Empty)
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            var strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" F_ID,vchtype,tamname,creator,creatime,state,fileinfo ");
            strSql.Append(" FROM Report_template ");
            if (strWhere.Trim() != string.Empty)
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            var strSql = new StringBuilder();
            strSql.Append("select count(1) FROM Report_template ");
            if (strWhere.Trim() != string.Empty)
            {
                strSql.Append(" where " + strWhere);
            }
            var obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            var strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby );
            }
            else
            {
                strSql.Append("order by T.F_ID desc");
            }
            strSql.Append(")AS Row, T.*  from Report_template T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }
    }
}

